// pages/route/route.js
const app = getApp()
const http = require("../../utils/http.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    WindowHeight: app.globalData.WindowHeight,
    ScreenHeight: app.globalData.ScreenHeight,

    next: false,

    topNum: 0,
    floorstatus: false,

    searchName: '',
    mddList: [],
    checkedList: [],
    mddLeft: 0,

    routeName: '',

    array: [],
    routeDayNum: 0,

    startDate: '',
    routeDate: '-- -- --',
    routeDateWeek: '',

    pageIndex: 1,
    pageSize: 10,
    totalPage: 0,

    version: '1.0.0'
  },

  onLoad() {
    http.get("/get")
      .then(res => {
        this.setData({
          version: res.data,
        })
      })

    let today = new Date();
    for (let i = 1; i < 100; i++) {
      this.data.array.push(i)
    }
    this.setData({
      array: this.data.array,
      startDate: today.getFullYear + '-' + today.getMonth + '-' + today.getDate
    })
  },
  onReady() {
    this.getData();
  },
  DateChange(e) {
    let date = new Date(e.detail.value)
    let routeDateWeek = date.getDay()
    let str = '周'
    switch (routeDateWeek) {
      case 0:
        str += "日";
        break;
      case 1:
        str += "一";
        break;
      case 2:
        str += "二";
        break;
      case 3:
        str += "三";
        break;
      case 4:
        str += "四";
        break;
      case 5:
        str += "五";
        break;
      case 6:
        str += "六";
        break;
    }
    this.setData({
      routeDate: e.detail.value,
      routeDateWeek: str
    })
  },
  inputChange(e) {
    this.setData({
      searchName: e.detail.value
    })
    this.getData()
  },
  clearSearchName() {
    this.setData({
      searchName: ''
    })
    this.getData()
  },
  getData() {
    this.setData({
      pageIndex: 1
    })
    let param = {
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize,
      searchName: this.data.searchName
    }
    http.get("/mdd/getMdd", param)
      .then(res => {
        if (res.data.status == 200) {
          let list = this.data.checkedList
          let list2 = res.data.data.resultList
          for (let i = 0; i < list2.length; i++) {
            delete list2[i].checked
            for (let j = 0; j < list.length; j++) {
              if (list2[i].mddId == list[j].mddId) {
                list2[i].checked = true
              }
            }
          }
          this.setData({
            mddList: list2,
            checkedList: list,
            totalPage: res.data.data.totalPage
          })
        } else {
          this.setData({
            mddList: [],
            totalPage: 0
          })
        }
      })
  },
  toLowFun(e) {
    if (this.data.pageIndex >= this.data.totalPage) {
      return;
    }
    this.setData({
      pageIndex: ++this.data.pageIndex
    })
    let param = {
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize,
      searchName: this.data.searchName
    }
    http.get("/mdd/getMdd", param)
      .then(res => {
        if (res.data.status == 200) {
          let list = this.data.checkedList
          let list2 = this.data.mddList
          list2 = list2.concat(res.data.data.resultList)
          for (let i = 0; i < list2.length; i++) {
            delete list2[i].checked
            for (let j = 0; j < list.length; j++) {
              if (list2[i].mddId == list[j].mddId) {
                list2[i].checked = true
              }
            }
          }
          this.setData({
            mddList: list2,
            checkedList: list,
            totalPage: res.data.data.totalPage
          })
        } else {
          this.setData({
            totalPage: res.data.data.totalPage
          })
        }
      })
  },
  //回到顶部
  goTop: function(e) { // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },
  deletemdd(e) {
    let list = this.data.checkedList
    let list2 = this.data.mddList
    list.splice(e.currentTarget.dataset.index, 1)
    for (let i = 0; i < list2.length; i++) {
      delete list2[i].checked
      for (let j = 0; j < list.length; j++) {
        if (list2[i].mddId == list[j].mddId) {
          list2[i].checked = true
        }
      }
    }
    this.setData({
      mddList: list2,
      checkedList: list
    })
  },
  radioChange: function(e) {
    let list = this.data.checkedList
    if (e.detail.value.length == 0) {
      for (let i = 0; i < list.length; i++) {
        if (list[i].mddId == e.currentTarget.dataset.mddid) {
          list.splice(i, 1)
        }
      }
      this.setData({
        checkedList: list
      })
      return;
    }
    list.push({
      mddId: e.currentTarget.dataset.mddid,
      name: e.currentTarget.dataset.name
    })
    this.setData({
      checkedList: list,
      mddLeft: list.length * 1000
    })
  },
  nextSteps(e) {
    let list = this.data.checkedList
    let name = '';
    if (list.length < 3) {
      for (let i = 0; i < list.length; i++) {
        if(i==0){
          name += list[i].name
        }else{
          name +='、' + list[i].name
        }
      }
      name += '之旅'
    }
    this.setData({
      floorstatus: false,
      routeName: name,
      next: true
    })
  },
  lastSteps(e) {
    this.setData({
      next: false
    })
  },
  finish(e){
    let poiMddList = this.data.checkedList
    let startDayDate = this.data.routeDate
    let title = this.data.routeName
    let daytime = this.data.array[this.data.routeDayNum]
    if (title == ''){
      wx.showToast({
        title: '请输入行程名称',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    if (startDayDate == '-- -- --'){
      wx.showToast({
        title: '请选择出发日期',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    let param = {
      route: {
        title: title,
        daytime: daytime,
        startDayDate: startDayDate
      },
      poiMddList: poiMddList
    }
    http.post("/route/insert", {
      param: JSON.stringify(param)
    }).then(res=>{
      if(res.data.status == 200){
        wx.showToast({
          title: res.data.message,
          icon: 'success',
          duration: 2000,
          success:()=>{
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }
        })
      }else{
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  bindscroll(e) {
    if (e.detail.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  bindPickerChange: function(e) {
    this.setData({
      routeDayNum: e.detail.value
    })
  },
  bindInputName: function(e) {
    this.setData({
      routeName: e.detail.value
    })
  }
})